package pers.cz.controller;

import cn.hutool.core.date.DateUtil;
import pers.cz.model.AutoPublicTemplate;
import pers.cz.pub.RespBean;
import pers.cz.service.PublicTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @program: PostGirl-panent
 * @description: PublicTemplateController
 * @author: Cheng Zhi
 * @create: 2021-11-08 21:00
 **/
@RestController
@Api(tags = "公共模板管理接口")
@RequestMapping(path = "/api/publicTemplate")
public class PublicTemplateController {

    @Autowired
    PublicTemplateService service;

    @ApiOperation(value = "分页查询所有公共模板")
    @PostMapping(path = "/selectAllTemplateByPage")
    public List<AutoPublicTemplate> selectAllTemplateByPage(@RequestBody Map map) {

        int pageStart = (int) map.get("pageStart");
        int pageLimit = (int) map.get("pageLimit");

        return service.selectAllTemplateByPage(pageStart, pageLimit);
    }

    @ApiOperation(value = "获取模板总数")
    @GetMapping(path = "/selectCountTemplate")
    public Integer selectCountTemplate() {

        Integer count = service.selectCountTemplate();
        return count;
    }

    @ApiOperation(value = "添加公共模板")
    @PostMapping(path = "/addTemplate")
    public RespBean addTemplate(@RequestBody Map map) {

        String templateName = (String) map.get("templateName");
        String template = (String) map.get("template");

        AutoPublicTemplate publicTemplate = new AutoPublicTemplate();
        publicTemplate.setTemplate(template);
        publicTemplate.setTemplateName(templateName);
        publicTemplate.setUpdateTime(DateUtil.date());

        Integer count = service.addTemplate(publicTemplate);

        return RespBean.ok("成功添加[" + count + "]条");
    }

    @ApiOperation(value = "修改公共模板")
    @PostMapping(path = "/updateTemplate")
    public RespBean updateTemplate(@RequestBody Map map) {

        Integer id = (Integer) map.get("id");
        String templateName = (String) map.get("templateName");
        String template = (String) map.get("template");

        AutoPublicTemplate publicTemplate = new AutoPublicTemplate();
        publicTemplate.setId(id);
        publicTemplate.setTemplate(template);
        publicTemplate.setTemplateName(templateName);
        publicTemplate.setUpdateTime(DateUtil.date());

        Integer count = service.updateTemplate(publicTemplate);

        return RespBean.ok("成功修改[" + count + "]条");
    }

    @ApiOperation(value = "删除公共模板")
    @PostMapping(path = "/deleteTemplate")
    public RespBean deleteTemplate(@RequestBody Map map) {

        Integer id = (Integer) map.get("id");
        Integer count = service.deleteTemplate(id);
        return RespBean.ok("成功删除[" + count + "]条");
    }

    @ApiOperation(value = "查询所有模板名称")
    @GetMapping(path = "/queryDistinctTemplateName")
    public List<AutoPublicTemplate> queryDistinctTemplateName() {

        return service.queryAllPublicTemplateName();
    }

    @ApiOperation(value = "模糊查询模板")
    @PostMapping(path = "/selectPublicTemplateByNameOrId")
    public List<AutoPublicTemplate> selectPublicTemplateByNameOrId(@RequestBody Map map) {

        String templateName = (String) map.get("templateName");
        String id = (String) map.get("id");

        return service.selectPublicTemplateByNameOrId(templateName, Integer.valueOf(id));
    }

    @ApiOperation(value = "解析模板中的参数")
    @PostMapping(path = "/selectPublicParamsById")
    public List<String> selectPublicParamsById(@RequestBody Map map) {
        Integer id = (Integer) map.get("id");
        return service.selectParamsById(id);
    }
}
